package com.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import com.util.BODY;
import com.util.ConfigUtil;
import com.util.HttpRequest;
import com.util.JSONUtil;
import com.util.OpenKey;
import com.util.ReadConfig;
import com.util.SessionInfo;

/**
 * 注册
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/zccontroller")
public class ZCcontroller {

	private static final Logger logger = Logger.getLogger(ZCcontroller.class);
	Map map  = new HashMap();
	List list = new ArrayList();
	  
	@RequestMapping("/test")
	@ResponseBody
	public String entryinterface(HttpServletRequest request, HttpServletResponse response,HttpSession session, String sfzh,String gjjzh,String ylsjh,String dxyz,String password) throws IOException{
		SessionInfo sessionIn = (SessionInfo) session.getAttribute(ConfigUtil.getSessionInfoName());

		//读取配置文件路劲
		String interfaceUrl = ReadConfig.readValue();
		Map remap = new HashMap();
		BODY body = new BODY();
		Date date=new Date();
		String Key="";
		// 接口编码
				body.setPassWd("123456");
				remap.put("AUTHORITY", "YBGJJ100");
				// 接口参数
				remap.put("LOGUserName", "admin");
				remap.put("BODY", body);
				String json3 = JSONUtil.ToJsongString2(remap);
				// 请求
				String result2 = HttpRequest.doPost(interfaceUrl, json3);
				logger.info("接口返回内容:" + result2);
				Map retMap2 = JSONUtil.toMap(result2);
				Map mx2 = JSONUtil.toMap(JSONUtil.ToJsongString2(retMap2.get("body")));
				List jx = (List) mx2.get("RETDATA");
				Map m = (Map) jx.get(0);
				Key += m.get("OpenKey").toString();		

		String Category="1";
		Map reqMap = new HashMap();
		BODY b=new BODY();
		OpenKey opk=new OpenKey();
		//接口编码
		reqMap.put("AUTHORITY", "YBGJJ173");
		//接口参数
		reqMap.put("LOGUserName","admin");//假数据
		reqMap.put("OpenKey",Key);
		b.setZJHM(sfzh);
		b.setGRZH(gjjzh);
		b.setPASSWORD(password);
		b.setSJHM(ylsjh);
		b.setCategory(Category);
		b.setDXYZM(dxyz);
		reqMap.put("BODY",b);
		String result=opk.getOpenKey(reqMap, session);
		Map resMap = JSONUtil.toMap(result);
		logger.info("接口返回内容:"+result);
		if(result != null){
			if(resMap.get("RETURNCODE").equals("100")){
				Map mx=JSONUtil.toMap(JSONUtil.ToJsongString2(resMap.get("body")));
				map.put("code", 0); 
				map.put("msg", null);
				map.put("data", mx.get("RETDATA"));
				response.setHeader("Content-type", "text/html;charset=UTF-8");
				response.setCharacterEncoding("UTF-8");
				String jsonString = JSONUtil.ToJsongString2(map);
				PrintWriter pw = response.getWriter();
				pw.write(jsonString);
			}
		}
		return null;
	}
	@RequestMapping("/dx")
	@ResponseBody
	public String dx(HttpServletRequest request, HttpServletResponse response,HttpSession session, String sfzh,String gjjzh,String sjhm) throws IOException{
		SessionInfo sessionIn = (SessionInfo) session.getAttribute(ConfigUtil.getSessionInfoName());

		//读取配置文件路劲
		String interfaceUrl = ReadConfig.readValue();
		Map remap = new HashMap();
		BODY body = new BODY();
		Date date=new Date();
		String Key="";
		// 接口编码
				body.setPassWd("123456");
				remap.put("AUTHORITY", "YBGJJ100");
				// 接口参数
				remap.put("LOGUserName", "admin");
				remap.put("BODY", body);
				String json3 = JSONUtil.ToJsongString2(remap);
				// 请求
				String result2 = HttpRequest.doPost(interfaceUrl, json3);
				logger.info("接口返回内容:" + result2);
				Map retMap2 = JSONUtil.toMap(result2);
				Map mx2 = JSONUtil.toMap(JSONUtil.ToJsongString2(retMap2.get("body")));
				List jx = (List) mx2.get("RETDATA");
				Map m = (Map) jx.get(0);
				Key += m.get("OpenKey").toString();		

		Map reqMap = new HashMap();
		BODY b=new BODY();
		OpenKey opk=new OpenKey();
		//接口编码
		reqMap.put("AUTHORITY", "YBGJJ174");
		//接口参数
		reqMap.put("LOGUserName","admin");//假数据
		reqMap.put("OpenKey",Key);
		b.setGRZH(gjjzh);
		b.setZJHM(sfzh);
		b.setSJHM(sjhm);
		reqMap.put("BODY",b);
		String result=opk.getOpenKey(reqMap, session);
		Map resMap = JSONUtil.toMap(result);
		logger.info("接口返回内容:"+result);
		if(result != null){
			if(resMap.get("RETURNCODE").equals("100")){
				Map mx=JSONUtil.toMap(JSONUtil.ToJsongString2(resMap.get("body")));
				map.put("code", 0); 
				map.put("msg", null);
				map.put("data", mx.get("RETDATA"));
				response.setHeader("Content-type", "text/html;charset=UTF-8");
				response.setCharacterEncoding("UTF-8");
				String jsonString = JSONUtil.ToJsongString2(map);
				PrintWriter pw = response.getWriter();
				pw.write(jsonString);
			}
		}
		return null;
	}
	
	@RequestMapping("/dwzc")
	@ResponseBody
	public String dwzc(HttpServletRequest request, HttpServletResponse response,HttpSession session, String sfzh,String gjjzh,String ylsjh,String dxyz,String password) throws IOException{
		SessionInfo sessionIn = (SessionInfo) session.getAttribute(ConfigUtil.getSessionInfoName());

		//读取配置文件路劲
		String interfaceUrl = ReadConfig.readValue();
		Map remap = new HashMap();
		BODY body = new BODY();
		Date date=new Date();
		String Key="";
		// 接口编码
				body.setPassWd("123456");
				remap.put("AUTHORITY", "YBGJJ100");
				// 接口参数
				remap.put("LOGUserName", "admin");
				remap.put("BODY", body);
				String json3 = JSONUtil.ToJsongString2(remap);
				// 请求
				String result2 = HttpRequest.doPost(interfaceUrl, json3);
				logger.info("接口返回内容:" + result2);
				Map retMap2 = JSONUtil.toMap(result2);
				Map mx2 = JSONUtil.toMap(JSONUtil.ToJsongString2(retMap2.get("body")));
				List jx = (List) mx2.get("RETDATA");
				Map m = (Map) jx.get(0);
				Key += m.get("OpenKey").toString();		

		String Category="3";
		Map reqMap = new HashMap();
		BODY b=new BODY();
		OpenKey opk=new OpenKey();
		//接口编码
		reqMap.put("AUTHORITY", "YBGJJ175");
		//接口参数
		reqMap.put("LOGUserName","admin");//假数据
		reqMap.put("OpenKey",Key);
		b.setDWZH(gjjzh);
		b.setPASSWORD(password);
		b.setSJHM(ylsjh);
		b.setCategory(Category);
		b.setDXYZM(dxyz);
		reqMap.put("BODY",b);
		String result=opk.getOpenKey(reqMap, session);
		Map resMap = JSONUtil.toMap(result);
		logger.info("接口返回内容:"+result);
		if(result != null){
			if(resMap.get("RETURNCODE").equals("100")){
				Map mx=JSONUtil.toMap(JSONUtil.ToJsongString2(resMap.get("body")));
				map.put("code", 0); 
				map.put("msg", null);
				map.put("data", mx.get("RETDATA"));
				response.setHeader("Content-type", "text/html;charset=UTF-8");
				response.setCharacterEncoding("UTF-8");
				String jsonString = JSONUtil.ToJsongString2(map);
				PrintWriter pw = response.getWriter();
				pw.write(jsonString);
			}
		}
		return null;
	}

	@RequestMapping("/dwdx")
	@ResponseBody
	public String dwdx(HttpServletRequest request, HttpServletResponse response,HttpSession session, String DWZH,String SJHM) throws IOException{
		SessionInfo sessionIn = (SessionInfo) session.getAttribute(ConfigUtil.getSessionInfoName());

		//读取配置文件路劲
		String interfaceUrl = ReadConfig.readValue();
		Map remap = new HashMap();
		BODY body = new BODY();
		Date date=new Date();
		String Key="";
		// 接口编码
				body.setPassWd("123456");
				remap.put("AUTHORITY", "YBGJJ100");
				// 接口参数
				remap.put("LOGUserName", "admin");
				remap.put("BODY", body);
				String json3 = JSONUtil.ToJsongString2(remap);
				// 请求
				String result2 = HttpRequest.doPost(interfaceUrl, json3);
				logger.info("接口返回内容:" + result2);
				Map retMap2 = JSONUtil.toMap(result2);
				Map mx2 = JSONUtil.toMap(JSONUtil.ToJsongString2(retMap2.get("body")));
				List jx = (List) mx2.get("RETDATA");
				Map m = (Map) jx.get(0);
				Key += m.get("OpenKey").toString();		

		Map reqMap = new HashMap();
		BODY b=new BODY();
		OpenKey opk=new OpenKey();
		//接口编码
		reqMap.put("AUTHORITY", "YBGJJ176");
		//接口参数
		reqMap.put("LOGUserName","admin");//假数据
		reqMap.put("OpenKey",Key);
		b.setDWZH(DWZH);
		b.setSJHM(SJHM);
		reqMap.put("BODY",b);
		String result=opk.getOpenKey(reqMap, session);
		Map resMap = JSONUtil.toMap(result);
		logger.info("接口返回内容:"+result);
		if(result != null){
			if(resMap.get("RETURNCODE").equals("100")){
				Map mx=JSONUtil.toMap(JSONUtil.ToJsongString2(resMap.get("body")));
				map.put("code", 0); 
				map.put("msg", null);
				map.put("data", mx.get("RETDATA"));
				response.setHeader("Content-type", "text/html;charset=UTF-8");
				response.setCharacterEncoding("UTF-8");
				String jsonString = JSONUtil.ToJsongString2(map);
				PrintWriter pw = response.getWriter();
				pw.write(jsonString);
			}
		}
		return null;
	}
}
